<?php

namespace App\Http\Controllers\Admin;


use App\Http\Models\Member;

class MemberController extends CommonController{
   
    public function index(){
        $data = User::orderBy('member_createtime', 'desc')->paginate(15);
        return view('admin.member.index', ['data' => $data]);
    }
    
    public function create(){
        return view('admin.member.add');
    }    
    
    public function store(){
        $input = \Input::except('_token');
        if ($input) {
            $rules = [
                'member_name' => 'required|unique:member,member_name',
                'member_email' => 'email',
                'member_password' => 'required|min:6|confirmed'
            ];

            $messages = [
                'member_name.required' => '会员名称不能为空！',
                'member_name.unique' => '会员名称必须唯一！',
                'member_email.email' => '会员邮箱格式不正确！',
                'member_password.required' => '会员密码不能为空！',
                'member_password.min' => '会员密码太短，不到6位！',
                'member_password.confirmed' => '确认密码与密码不匹配'
            ];
            
            $validator = \Validator::make($input, $rules, $messages);
            if ($validator->passes()) {
                $submit['member_name'] = $input['member_name'];
                $submit['member_email'] = $input['member_email'];
                $submit['member_password'] = md5($input['member_password']);
                $submit['member_createtime'] = time();
                $submit['member_updatetime'] = time();
                $submit['member_order'] = $input['member_order'];
                
                $r = User::create($submit);
                if($r)
                    return redirect ('admin/member');
                else 
                    return back()->with ('errors', '创建失败，请稍后再试！');
            } else
                return back()->withErrors($validator);
        }
    }

    public function show(){
        
    }

    public function edit($member_id){
        $data = User::find($member_id);
        return view('admin.member.edit', ['data' => $data]);
    } 
    
    public function update($member_id){
        $input = \Input::except('_token', '_method');
        
        $rules = [
                'member_email' => 'email',
                'old_member_password' => 'required',
                'member_password' => 'required|min:6|confirmed'
            ];

        $messages = [
                'member_email.email' => '会员邮箱格式不正确！',
                'member_password.required' => '会员密码不能为空！',
                'old_member_password.required' => '会员原密码不能为空！',
                'member_password.min' => '会员密码太短，不到6位！',
                'member_password.confirmed' => '确认密码与密码不匹配'
            ];

        $validator = \Validator::make($input, $rules, $messages);
        $member = User::find($member_id);
        $oldpassword = md5($input['old_member_password']);
        $validator->after(function($validator) use ($oldpassword, $member) {
            if ($member->member_password != $oldpassword) {
                $validator->errors()->add('errors', '原密码错误');
        }});
        
        if ($validator->fails()) 
            return back()->withErrors($validator);
        
        $submit['member_email'] = $input['member_email'];
        $submit['member_password'] = md5($input['member_password']);
        $submit['member_updatetime'] = time();
        $r = $member->update($submit);
        
        return back()->with('errors', ($r) ? '会员更新成功':'会员更新失败，请稍后再试！');
    }

    public function destroy($member_id){
        $r = User::where('member_id', $member_id)->delete();
        
        $data = [];
      
        $data['status'] = ($r) ? 0 : 1;
        $data['msg'] = ($r) ? '删除成功':'删除失败请稍后再试！';

        return $data;  
    }
    
}
